﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public enum state
{
	XUANZHUAN,
	SHENCHANG,
	SHOUHUI
}
public class Game : MonoBehaviour {
	public GameObject shengzi;
	public float rota=0.07f;
	public state GameState= state.XUANZHUAN;
	public float Nonespeed=0.02f;
	public Text sourcetext;
	public Text Highesttext;
	public Text winhighesttext;
	public Text winsourcetext;
	public Text scoretext;
	public Text timetext;
	public Text dalitext;
	public Text boomtext;
	public Text timeyaotext;
	public Text shopgoldtext;
	public Text informationtext;
	public int source=0;
	public int dalicount=2;
	public int boomcount=2;
	public int timecount=2;
	public GameObject Dragobject;
	public GameObject Booma;
	public GameObject Goutran;
	public float timego=60;
	public GameObject GameoverPanel;
	public GameObject ShopPanel;
	public bool pause=false;
	public bool Gameover=false;
	public GameObject WingamePanel;
	public bool Winover=false;
//	public void Showtime()
//	{
//	
//	}
	public void Entertriggerdali()
	{
		informationtext.text = "大力药水，单价50，用于拖拽金矿时进行加速\n";
	}
	public void exitdali()
	{
		informationtext.text = "";
	}

	public void Enterboom()
	{
		informationtext.text = "炸弹，单价50，用于拖拽金矿时销毁物体\n";
	}
	public void exitboom()
	{
		informationtext.text = "";
	}

	public void Entertimeyaoshui()
	{
		informationtext.text = "时间药剂，单价100，用于增加剩余时间";
	}
	public void exittimeyao()
	{
		informationtext.text = "";
	}

	public void Onclickshopdali()
	{   
		if (source >= 50) 
		{
			source -= 50;
			showsource();
			showshopgold();
			dalicount++;
			dalitext.text = "" + dalicount;
		}
		else
		{
			informationtext.text="马化腾：只要你充够钱，你就能变得无比强大\n";
		}
	}
	public void Onclickshopzhadan()
	{   
		if (source >= 50) 
		{
			source -= 50;
			showsource();
			showshopgold();
			boomcount++;
			boomtext.text = "" + boomcount;
		}
		else
		{
			informationtext.text="马化腾：只要你充够钱，你就能变得无比强大\n";
		}
	}
	public void Onclickshoptime()
	{   
		if (source >= 100) 
		{
			source -= 100;
			showsource();
			showshopgold();
			timecount++;
			timeyaotext.text=""+timecount;
		}
		else
		{
			informationtext.text="马化腾：只要你充够钱，你就能变得无比强大\n";
		}
	}
	public void showdalitext()
	{
		dalitext.text = "" + dalicount;
	}
	public void showboomtext()
	{
		boomtext.text = "" + boomcount;
	}
	public void showtimeText()
	{
		timeyaotext.text = "" + timecount;
	}
	public void showshopgold()
	{
		shopgoldtext.text = "" + source;
	}
	public void OnClickstop()
	{   
		if (!pause) 
		{
		//ButtonPanel.SetActive = false;
		//MenuPanel.SetActive = true;
			pause = true;
			ShopPanel.SetActive(true);
			shopgoldtext.text=""+source;
		} 
		else
		{
			pause=false;
		}
	}

	public void Onclickbackshop()
	{
		ShopPanel.SetActive (false);
		pause = false;
	}
	public void Onclickmenu()
	{
		Application.LoadLevel("MENU");
	}
//	public void OnClickback()
//	{
//		MenuPanel.SetActive = false;
//		ButtonPanel.SetActive = true;
//		pause = false;
//	}
	public void OnclickNextGame()
	{
		Application.LoadLevel("Gold Miner");
	}

	public void showsource()
	{
		sourcetext.text = "" + source;
	}

	public static Game instance;
	void Awake()
	{
		Screen.SetResolution (MENU.Width,MENU.Height,false);
		instance = this;
	}
	public void dali()
	{
		if (Game.instance.GameState == state.SHOUHUI&&dalicount>0&&Dragobject!=null) 
		{
			Nonespeed = 0.01f;
			dalicount--;
			showdalitext();
		}
	}
	public void baozha()
	{
		if (Game.instance.GameState == state.SHOUHUI && boomcount > 0 && Dragobject != null) {
			GameObject obj = GameObject.Instantiate (Booma)as GameObject;
			obj.transform.position = Goutran.transform.position;
			Destroy (Dragobject);
			showsource();
			Nonespeed = 0.01f;
			boomcount--;
			showboomtext();
		}
	}
	public void Onclicktimegoy()
	{
		if (timecount > 0&&timego>0)
		{
			timego += 10;
			timecount--;
			timetext.text = "" + timego;
			showtimeText ();
		}
	}
	// Use this for initialization
	void Start ()
	{
		GameoverPanel.SetActive (false);
		WingamePanel.SetActive (false);
	}
	//public void 
	
	// Update is called once per frame
	void Update ()
	{
		if (!pause)
		{
			timego-=Time.deltaTime;
			int t=int.Parse(timego.ToString("0"));
			timetext.text=""+t;
			if (Input.GetKeyDown(KeyCode.DownArrow)&&GameState==state.XUANZHUAN)
			{   
				GameState=state.SHENCHANG;
			}
			
			if (GameState == state.XUANZHUAN) {
				this.transform.Rotate (new Vector3 (0, 0, rota));
				if (this.transform.eulerAngles.z > 60 && this.transform.eulerAngles.z < 180 || this.transform.eulerAngles.z >180 && this.transform.eulerAngles.z < 300) 
				{
					rota = -rota;
				}
			} 
			
			else if (GameState == state.SHENCHANG) 
			{
				
				shengzi.transform.localScale += new Vector3 (0, Nonespeed, 0);
			}
			
			else if(GameState == state.SHOUHUI)
			{
				shengzi.transform.localScale-=new Vector3(0,Nonespeed,0);
				if(shengzi.transform.localScale.y<1)
				{
					shengzi.transform.localScale=new Vector3(1,1,1);
					GameState=state.XUANZHUAN;
				}
			}
			GameBehaviour[]GameManager=FindObjectsOfType<GameBehaviour>();
			for(int i =0;i<GameManager.Length;i++)
			{
				GameManager[i].GameUpdate();
				
			}
			if(timego<=0)
			{   
				if(source<800)
				{
				Gameover=true;
				GameoverPanel.SetActive(true);
				scoretext.text=""+source;
				
				int highest=PlayerPrefs.GetInt("Highest",0);
				if(highest<source)
				{
					Highesttext.text=""+source;
					PlayerPrefs.SetInt("Highest",source);
				}
				else
				{
					Highesttext.text=""+highest;
				}
				}
				if(source>=800)
				{
					Winover=true;
					WingamePanel.SetActive(true);
					winsourcetext.text=""+source;
				int highest=PlayerPrefs.GetInt("Highest",0);
				if(highest<source)
					{
						winhighesttext.text=""+source;
						PlayerPrefs.SetInt("Highest",source);
					}
					else
					{
						winhighesttext.text=""+highest;
					}
				}
				
				

				pause=true;

			}
		}
	}
}

